package logic

import (
	"context"
	"errors"
	"houses_srv/models"
	"time"

	"houses_srv/houses"
	"houses_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHouseLogic {
	return &UpdateHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateHouseLogic) UpdateHouse(in *houses.UpdateHouseRequest) (*houses.UpdateHouseResponse, error) {
	// todo: add your logic here and delete this line

	var h models.House
	h.ID = uint(in.HouseId)

	update := h.CreatedAt.Add(5 * time.Minute)

	if update.Unix() < time.Now().Unix() {
		logx.Error("修改", errors.New("超过5分钟则不允许"))
		return nil, errors.New("超过5分钟则不允许")
	} else {
		h.Title = in.Title
		h.Image = in.Image
		h.Price = int(in.Price)
		h.Fool = float64(in.Fool)
		h.HouseType = in.HouseType
		err := h.UpdateHouse()
		if err != nil {
			logx.Error("修改失败", err)
			return nil, err
		}
	}

	return &houses.UpdateHouseResponse{HouseId: int64(h.ID)}, nil
}
